package se.kth.fericss.rts;
import java.awt.*;

import se.kth.fericss.menu.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.geom.Point2D;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.Color;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;

public class Game extends BasicGame
{

	public Game(String title) {
		super("");
		// TODO Auto-generated constructor stub
	}
	/**
	 * 
	 */
	
	private Robot robot;
    private Point mouseLocation;
    private Point centerLocation;
    private boolean relativeMouseMode;
    private boolean isRecentering;
    
    private World gameWorld;
	private View worldView;
	private TileMap tiles;
	private Minimap minimap;
	private Gui gui;
	private Input input;
	private Point dragSelect = null;
	private Dimension screenSize;
	private boolean quit = false;
	private boolean buildingMode = false;
	private boolean treeMode = false;
	private boolean dragView = false;
	private boolean paused = false;
	private int frameCount = 0; 
	private int updateCount = 0;
	
	private MainMenu menu;

	public static final boolean DEBUG_MODE = false;
	public static final String IMAGE_PATH = "se/kth/fericss/rts/images/";
	@Override
	public void init(GameContainer container) throws SlickException {
		container.setMinimumLogicUpdateInterval(20);
		container.setMaximumLogicUpdateInterval(20);
		container.setUpdateOnlyWhenVisible(false);
		container.setAlwaysRender(true);
		container.setSmoothDeltas(true);
		
		screenSize = new Dimension(container.getWidth(),container.getHeight());
		input = container.getInput();
		gameWorld = new World(this, new Dimension(2000, 2000));
		worldView = new View(gameWorld, screenSize);
		tiles = new TileMap(this, gameWorld, worldView);
		minimap = new Minimap(this, gameWorld, worldView);
		
		gui = new Gui(gameWorld, screenSize);
		gameWorld.init(gui);
		gameWorld.update(0);
		menu = new MainMenu(this,gameWorld,worldView);
		dragSelect=new Point();
		
	}
	@Override
	public void update(GameContainer container, int delta)
			throws SlickException {
		if(quit) {
			container.exit();
		}
		/////Move with arrow keys
		if(input.isKeyDown(Input.KEY_LEFT)) {
			worldView.setXScrollDirection(View.ScrollX.LEFT);
		} else if(input.isKeyDown(Input.KEY_RIGHT)) {
			worldView.setXScrollDirection(View.ScrollX.RIGHT);
		}
		else{
			worldView.setXScrollDirection(View.ScrollX.NONE);
		}
		if(input.isKeyDown(Input.KEY_DOWN)) {
			worldView.setYScrollDirection(View.ScrollY.DOWN);
		} else if(input.isKeyDown(Input.KEY_UP)) {
			worldView.setYScrollDirection(View.ScrollY.UP);
		}
		else{
			worldView.setYScrollDirection(View.ScrollY.NONE);
		}
		//////
		
		if(input.isKeyPressed(Input.KEY_ESCAPE)){
			menu.setVisible(!menu.isVisible());
			
		}
		if(menu.isVisible()){
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
				
			}
		}
		
		worldView.update();
		if(!paused) {
			gameWorld.update(delta);

			updateCount++;
		}
	}
	@Override
	public void render(GameContainer container, Graphics g) throws SlickException {
		g.translate(worldView.getViewLocationRect().getX(), worldView.getViewLocationRect().getY());
		g.setWorldClip(-worldView.getViewLocationRect().getX(), -worldView.getViewLocationRect().getY(), worldView.getViewLocationRect().getWidth(), worldView.getViewLocationRect().getHeight());

		tiles.draw(g);
		gameWorld.render(g);

		int mouseWorldX = input.getMouseX() - (int)worldView.getCurrentViewLocation().x;
		int mouseWorldY = input.getMouseY() - (int)worldView.getCurrentViewLocation().y;

		if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
			g.setColor(Color.white);
			g.drawRect(dragSelect.x, dragSelect.y, mouseWorldX - dragSelect.x, mouseWorldY - dragSelect.y);
		}
		else{
			dragSelect.x=input.getMouseX() - (int)worldView.getCurrentViewLocation().x;
			dragSelect.y=input.getMouseY() - (int)worldView.getCurrentViewLocation().y;
		}

		//Show where the building being built will be placed
		/*if(buildingMode) {
			Building building = new VillageCenter(gameWorld, new Point(mouseWorldX, mouseWorldY), gameWorld.getPlayerTeam());
			building.translate(-building.getWidth() / 2, -building.getHeight() / 2);

			if(building.checkCollision() == null) {
				g.setColor(Color.white);
			} else {
				g.setColor(Color.red);
			}

			g.setLineWidth(3);
			g.drawRect(mouseWorldX - building.getWidth() / 2, mouseWorldY - building.getHeight() / 2, building.getWidth(), building.getHeight());
			g.setLineWidth(1);
		}*/

		//Draw text and GUI in screen coordinates
		g.resetTransform();

		gui.draw(g);
		minimap.draw(g);
		if(menu.isVisible()){
			menu.draw(g);
		}

		frameCount++;
	}
	public static void main(String[] args) {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		try {
			AppGameContainer app = new AppGameContainer(new Game("TestGame"));
			app.setVerbose(DEBUG_MODE);

			if(DEBUG_MODE) {
				System.out.println("Supports alpha in backbuffer: " + app.supportsAlphaInBackBuffer());
				System.out.println("Supports multi-sampling: " + app.supportsMultiSample());
			}

			app.setVSync(true);
			//app.setDisplayMode(d.width-100, d.height-100, false);
			app.setDisplayMode(1200, 800, false);
			app.start();
		} catch(SlickException e) {
			e.printStackTrace();
		}
	}
	
}
